package com.cloud.business.controller.sys;


import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.business.controller.BaseController;
import com.cloud.business.entity.sys.SysRole;
import com.cloud.business.model.commonvm.DeletePost;
import com.cloud.business.model.commonvm.ResultMsg;
import com.cloud.business.model.sys.sysrolevm.SysRoleQueryModel;
import com.cloud.business.model.sys.sysrolevm.SysRoleVm;
import com.cloud.business.model.sys.sysrolevm.UpdateRoleUsersPost;
import com.cloud.business.service.sys.ISysRoleService;
import com.cloud.core.common.HttpContentResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统角色 前端控制器
 * </p>
 *
 * @author dh
 * @since 2020-03-21
 */
@RestController
@RequestMapping("/api/sysRole")
public class SysRoleController extends BaseController {

    @Autowired
    private ISysRoleService ISysRoleService;

    //region 获取分页列表

    /**
     * 获取分页列表
     *
     * @param queryModel
     * @return
     */
    @PostMapping("/getListByPage")
    public HttpContentResult getListByPage(@RequestBody SysRoleQueryModel queryModel) {
        ResultMsg msg = new ResultMsg();
        try {
            queryModel.setOperationId(getOperationId());
            IPage<SysRoleVm> page = new Page<>(queryModel.getPageNum(), queryModel.getRecordNum());
            ISysRoleService.getListByPage(queryModel, page, msg);
            return HttpContentResult.successResult(msg.getMsg(), page.getTotal(), page.getRecords());
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 创建

    /**
     * 创建
     *
     * @param model
     * @return
     * @author Rex
     */
    @PostMapping("/create")
    public HttpContentResult create(@RequestBody SysRole model) {
        ResultMsg msg = new ResultMsg();
        try {
            model.setCreateUserId(getOperationId());
            Integer objectId = ISysRoleService.create(model, msg);
            return HttpContentResult.successResult(msg.getMsg(), 1L, objectId);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 更新

    /**
     * 更新
     *
     * @param model
     * @return
     * @author Rex
     */
    @PostMapping("/update")
    public HttpContentResult update(@RequestBody SysRole model) {
        ResultMsg msg = new ResultMsg();
        try {
            model.setModificationUserId(getOperationId());
            Integer objectId = ISysRoleService.update(model, msg);
            return HttpContentResult.successResult(msg.getMsg(), 1L, objectId);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 获取单条

    /**
     * 获取单条
     *
     * @param objectId
     * @return
     */
    @GetMapping("/getById")
    public HttpContentResult getById(Integer objectId) {
        ResultMsg msg = new ResultMsg();
        try {
            Integer operationId = getOperationId();
            SysRole sysRole = ISysRoleService.getById(operationId, objectId, msg);
            return HttpContentResult.successResult(msg.getMsg(), 1L, sysRole);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 批量删除

    /**
     * 批量删除
     *
     * @param deletePost
     * @return
     * @author Rex
     */
    @PostMapping("/deleteByList")
    public HttpContentResult deleteByList(@RequestBody DeletePost deletePost) {
        ResultMsg msg = new ResultMsg();
        try {
            deletePost.setOperationId(getOperationId());
            Integer count = ISysRoleService.deleteByList(deletePost, msg);
            return HttpContentResult.successResult(msg.getMsg(), 1L, count);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 更新角色用户

    /**
     * 更新角色用户
     *
     * @param updateRoleUsersPost
     * @return
     */
    @PostMapping("/updateRoleUsers")
    public HttpContentResult updateRoleUsers(@RequestBody UpdateRoleUsersPost updateRoleUsersPost) {
        ResultMsg msg = new ResultMsg();
        try {
            updateRoleUsersPost.setOperationId(getOperationId());
            Boolean flag = ISysRoleService.updateRoleUsers(updateRoleUsersPost, msg);
            return HttpContentResult.successResult(msg.getMsg(), 1L, flag);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion
    /**
     * 获取权限code
     * @param userId
     * @return
     */
    @GetMapping("/getSysRoleCode")
    public HttpContentResult getSysRoleCode(Integer userId) {
        ResultMsg msg = new ResultMsg();
        try {
            Integer operationId = getOperationId();
            Long dataCount = 0L;
            List<Map> list = ISysRoleService.getSysRoleCode(operationId, userId, msg);
            if (!CollectionUtils.isEmpty(list)) {
                dataCount = Convert.toLong(list.size());
            }
            return HttpContentResult.successResult(msg.getMsg(), dataCount, list);
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }

}
